package edu.wfmanage.models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.util.DB;

public class Lookup {
	private String table="";
	private int id;
	private String name="";
	private String email_id="";
	private String password="";
	private int is_current;
	public Lookup(){
		
	}
	public Lookup(int id, String name, int is_current, String table) {
		super();
		this.id = id;
		this.name = name;
		this.is_current = is_current;
		this.table=table;
	}
	public int getId() {
		return id;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getIs_current() {
		return is_current;
	}
	public void setIs_current(int is_current) {
		this.is_current = is_current;
	}
	
	public String getEmail_id() {
		return email_id;
	}
	public void setEmail_id(String email_id) {
		this.email_id = email_id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public static ArrayList<Lookup> find(String table,String selectionModifier) {
		ArrayList<Lookup> selection = new ArrayList<Lookup>();
		ResultSet resultSet = null;
		String query = "select "+table+"_id,"+table+"_name, is_current "+
				"from "+table+" "+ selectionModifier;
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				Lookup lookup = new Lookup();
				lookup.table=table;
				lookup.id= resultSet.getInt(table+"_id");
				lookup.name = resultSet.getString(table+"_name");
				lookup.is_current = resultSet.getInt("is_current");
				System.out.println("lookupname:"+lookup.name);
				System.out.println("iscurrent:"+lookup.is_current);
				selection.add(lookup);
			}
		} catch (SQLException e) {
           // MyLog.myCatch("Book.java", 43, e);
			e.printStackTrace();
		}
		//DB.close(resultSet);
		DB.close(connection);
		return selection;
	}
	public static Lookup findOne(String table,String selectionModifier) {
		ArrayList<Lookup> selection = new ArrayList<Lookup>();
		ResultSet resultSet = null;
		String query = "select "+table+"_id,"+table+"_name, is_current "+
				"from "+table+" "+ selectionModifier;
		if(table.equals("user")){
			query="select user_id,user_name,password,email_id,is_current from user "+selectionModifier;
		}
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		Lookup lookup = new Lookup();
		try {
			if (resultSet.next()) {
				
				lookup.table=table;
				lookup.id= resultSet.getInt(table+"_id");
				lookup.name = resultSet.getString(table+"_name");
				lookup.is_current = resultSet.getInt("is_current");
				if(table.equals("user")){
					lookup.email_id=resultSet.getString("email_id");
					lookup.password=resultSet.getString("password");
				}
				System.out.println("lookupname:"+lookup.name);
				System.out.println("iscurrent:"+lookup.is_current);
				}
		} catch (SQLException e) {
           // MyLog.myCatch("Book.java", 43, e);
			e.printStackTrace();
		}
		//DB.close(resultSet);
		DB.close(connection);
		return lookup;
	}
	public int update(String table) {
		String updateSQL = 
			    "update "+ table
				+ " set "+table+"_name = '" + name + "', is_current = " + is_current + 
				" where "+table+"_id = " + id;
		System.out.println(updateSQL);
		return DB.update(updateSQL);
	}

	public int insert(String table) {
		String insertSQL = "insert into "+table
				+ " ("+table+"_name, is_current) " + "values('" + name
				+ "', " + is_current + ");";
		System.out.println("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
	public static int findOnefordelete(String table,String selectionModifier) {
		String query = "delete from "+table+" "+ selectionModifier;
		System.out.println("query"+query);
		Connection connection = DB.getConnection();
		int i= DB.deleteFromDB(query, connection);
		DB.close(connection);
		return i;
	}
	public static ArrayList<Lookup> findWF(String selectionModifier) {
		ArrayList<Lookup> selection = new ArrayList<Lookup>();
		ResultSet resultSet = null;
		String query = "select dw.workflowpoint_id,workflowpoint_name from domain_workflowpoint dw,workflowpoint w " +
				"where dw.workflowpoint_id = w.workflowpoint_id and domain_id= "+selectionModifier;
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				Lookup lookup = new Lookup();
				lookup.id= resultSet.getInt("workflowpoint_id");
				lookup.name = resultSet.getString("workflowpoint_name");
				System.out.println("WFPointname:"+lookup.name);
				System.out.println("WFPointID:"+lookup.id);
				selection.add(lookup);
			}
		} catch (SQLException e) {
           // MyLog.myCatch("Book.java", 43, e);
			e.printStackTrace();
		}
		//DB.close(resultSet);
		DB.close(connection);
		return selection;
	}
}
